package com.neusoft.yuejian12.yuejian_12.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.neusoft.yuejian12.yuejian_12.R;

import com.neusoft.yuejian12.yuejian_12.activity.SearchTrainActivity;
import com.neusoft.yuejian12.yuejian_12.activity.TrainCainiaoActivity;
import com.neusoft.yuejian12.yuejian_12.activity.TrainGaojiActivity;
import com.neusoft.yuejian12.yuejian_12.activity.TrainJinjieActivity;
import com.neusoft.yuejian12.yuejian_12.activity.TrainRumenActivity;
import com.neusoft.yuejian12.yuejian_12.adapter.TrainListAdapter;
import com.neusoft.yuejian12.yuejian_12.javabean.TrainBean;
import com.neusoft.yuejian12.yuejian_12.listener.RetrofitListener;
import com.neusoft.yuejian12.yuejian_12.model.TrainModel;

import java.util.List;


/**
 * Created by Administrator on 2017/9/26.
 */

public class TrainFragment extends Fragment implements View.OnClickListener,RetrofitListener<List<TrainBean>> {
    private TextView t_name;
    private ImageView t_picture;
    private RecyclerView recyclerView;
    private View view = null;
    private RadioButton rumen;
    private RadioButton jinjie;
    private RadioButton cainiao;
    private RadioButton gaoji;
    private RollPagerView rollPV = null;
   private TrainListAdapter adapter;
    private ImageView search_train;
    private FragmentTransaction ft;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab01, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));//设置布局
        initView();
        //getData();
        getTrainlist();
        initEvents();
        return view;
    }

    private void initView() {
        t_name = (TextView) view.findViewById(R.id.name);
        t_picture = (ImageView) view.findViewById(R.id.t_picture);
        rumen = (RadioButton) view.findViewById(R.id.rumen);
        cainiao = (RadioButton) view.findViewById(R.id.cainiao);
        jinjie = (RadioButton) view.findViewById(R.id.jinjie);
        gaoji = (RadioButton) view.findViewById(R.id.gaoji);

        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
        search_train = (ImageView) view.findViewById(R.id.search_train);


    }

    public void initEvents() {
        //搜索
        search_train.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(TrainFragment.this.getActivity(), SearchTrainActivity.class);
                startActivity(intent);

            }
        });
        rumen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(TrainFragment.this.getActivity(), TrainRumenActivity.class);
                startActivity(intent);
            }
        });
        cainiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(TrainFragment.this.getActivity(), TrainCainiaoActivity.class);
                startActivity(intent);
            }
        });

        jinjie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(TrainFragment.this.getActivity(), TrainJinjieActivity.class);
                startActivity(intent);
            }
        });
        gaoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(TrainFragment.this.getActivity(), TrainGaojiActivity.class);
                startActivity(intent);
            }
        });


        //轮播图
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPV.setHintView(new ColorPointHintView(getActivity(),
                getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPV.setAdapter(new RollPagerAdapter());

        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/


    }

    private int images[] = {
            R.drawable.jianshen4,
            R.drawable.p3,
            R.drawable.p5
    };



    //设置适配器
    private class RollPagerAdapter extends StaticPagerAdapter {

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view = new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }
    void getTrainlist(){
        TrainModel clubModel=new TrainModel();
        clubModel.getTrainlist(this);
    }

    @Override
    public void onSuccess(List<TrainBean> trainBean){
        adapter = new TrainListAdapter(TrainFragment.this.getActivity(),trainBean);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFail() {
        //showToast("网络发生错误");
    }
    @Override
    public void onClick(View view) {

    }
    //获取列表数据
//    private void getData() {
//        TrainModel trainModel = new TrainModel();
//        Call<List<TrainBean>> call = trainModel.getTrainlist();
//        //System.out.print("t_name");
//        call.enqueue(new Callback<List<TrainBean>>() {
//            @Override
//            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
//                List<TrainBean> data = response.body();  //获取数据
//                // System.out.printf("data");
//                recyclerView.setAdapter(new TrainListAdapter(TrainFragment.this.getActivity(), data));
//            }
//
//            @Override
//            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
//
//            }
//        });
//
//    }
}




